<template>
	<view class="container position-relative padding-top-sm">
		<view v-for='(item,index) in list' :key='index'
			class="radius5  padding item bg-white margin-left-sm margin-right-sm margin-bottom-sm"  @click="handChange(item)">
			<view class="flex justify-between align-center">
				<!-- <view class="text-cut"> -->
					<view class="text-181818 text-bold text-15">
						{{item.receiver}} {{item.mobile}}
					</view>
					<view class="margin-top-xs text-13 text-666 text-cut">
						{{item.addressInfo}}
					</view>
				<!-- </view> -->
				<!-- <view class="cuIcon-right">

				</view> -->
			</view>
			<view class="flex justify-between align-center margin-top-xl">
				<view class="flex justify-start align-center">
					<image v-if='item.isdefault' :src="baseUrl + '/static/selected.png'"
						mode=""></image>
					<image v-else :src="baseUrl + '/static/unselected.png'" mode=""></image>
					<view class="text-13 margin-left-xs">
						默认地址
					</view>
				</view>
				<view class="text-999 flex justify-start align-center" 	@click.stop="toPage('/filePage/pages/address-add/address-add?id='+item.id)">
					<view class="cuIcon-delete">

					</view>
					<view class="text-13 margin-left-xs"
					>
						编辑
					</view>
				</view>
				<view class="text-999 flex justify-start align-center">
					<view class="cuIcon-delete">

					</view>
					<view class="text-13 margin-left-xs" @click.stop="deleteAddress(item.id)">
						删除
					</view>
				</view>
			</view>
		</view>
		<view class="text-15 text-white bg-color p-btn round flex justify-center align-center"
			@click="toPage('/filePage/pages/address-add/address-add')">
			添加新地址
		</view>
		<uni-load-more :status="loading" />
	</view>
</template>

<script>
	import BetterPull from '@/common/util/BetterPull'
	export default {
		mixins: [
			BetterPull({
				async getPage(page, done) {
					this.loading = 'loading';
					const obj = {
						pageNum: page,
						pageSize: 10,
						userId: uni.getStorageSync('userId')
					}
					const [err, res] = await this.$get(this.$api.addressList, obj)
					if (res) {
						var data = res.data.list
						done(data);
						if (data.length < 10) {
							this.loading = 'noMore';
						} else {
							this.loading = 'more';
						}

					}
					if (err) {
						done([]);
						this.loading = 'noMore';
					}
				}
			})
		],
		data() {
			return {
				baseUrl: this.$config.baseUrl,
				list: [],
				loading: "more",
				is_change:false
			};
		},
		onShow() {

		},
		onLoad(options) {
			if(options.is_change) this.is_change = options.is_change
			console.log(options)
		},
		methods: {
			toPage(url) {
				console.log(url)
				this.$util.openNewPage(url);
			},
			deleteAddress(id) {
				const that = this
				uni.showModal({
					title: '提示',
					content: '你确定删除该地址吗',
					success: function(res) {
						if (res.confirm) {
							console.log('用户点击确定');
							that.$get(that.$api.addressDelete, {
								addrId: id,
								userId: uni.getStorageSync('userId')
							}).then(res => {
								const [err, rs] = res
								if (rs) {
									that.$util.msg(rs.message)
									that.Refresh();
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			handChange(item) {
				// let title = ''
				// let status = ''
				if(this.is_change){
					uni.setStorage({
						key: "address",
						data: item,
					})
					uni.navigateBack()
					return
				}
				
				
				if (item.isdefault) {
				      return
 
				} 
				const that = this
				uni.showModal({
					title: '提示',
					content: '你确定将改地址设为默认地址吗',
					success: function(res) {
						if (res.confirm) {
							const data = {
								addrId: item.id,
								userId: uni.getStorageSync('userId'),
								

							}
							that.$get(that.$api.addressDefault, data).then(rs => {
								const [err, res] = rs
								if (res) {
									that.$util.msg(res.message)
									// that.Refresh();
									setTimeout(() =>{
									uni.navigateBack()	
									},1500)
								}

							})
						} else if (res.cancel) {

						}
					}
				});
			}

		}
	}
</script>

<style lang="scss" scoped>
	.p-btn {
		position: fixed;
		height: 78rpx;
		bottom: 20rpx;
		left: 20rpx;
		right: 20rpx;
		z-index: 1;
	}

	.item {
		image {
			width: 26rpx;
			height: 26rpx;
		}
	}
</style>
